#include "type.h"


void draw_graph(const Graph & g,
	std::ostream & c,
	std::string name,std::set<my_edge_t> special_edges = std::set<my_edge_t>(),
	std::set<my_vertex_t> special_vertices = std::set<my_vertex_t>()) {

		auto vcol =  [&g,&special_vertices] ( Graph::vertex_descriptor v) { return (special_vertices.find(g[v]) != special_vertices.end() ) ?  "[color=red]" : ""  ;} ;
		auto ecol =  [&g,& special_edges]  ( Graph::edge_descriptor v) { return (special_edges.find(g[v]) != special_edges.end() ) ?  "[color=blue]" : ""  ;} ;

		c << "Graph {" << std::endl;
		auto vertices = boost::vertices(g);
		auto edges = boost::edges(g);
		std::for_each(vertices.first,vertices.second,[&](Graph::vertex_descriptor vertex) { c << g[vertex] << vcol(vertex) << std::endl ;});
		std::for_each(edges.first,edges.second,[&](Graph::edge_descriptor edge) { c << g[edge.m_source] << " -- " << g[edge.m_target] <<  ecol(edge) << std::endl ;});
		c << "}";
};


void export_result(Graph & sol,
				   Graph & foret,
				   Graph & arbre,
				   std::string name = "KeitaFreminville")
{
	int k = boost::num_vertices(sol);
	int f = boost::num_vertices(foret);
	int t = boost::num_vertices(arbre);
	int f_t = f - t ;
	int m = boost::num_edges(sol);
	std::ofstream stream("G"+boost::lexical_cast<std::string>(k)+name);
	stream << k << " " << t << " " << f << " " << f_t << std::endl 
		   << m << std::endl ;
    
	auto edges = boost::edges(sol);
	std::set<my_edge_t> my_edge ;
	for(auto it = edges.first; it != edges.second;++it)
		my_edge.insert(sol[*it]);
	for(auto it = my_edge.begin(); it != my_edge.end();++it)
		stream << it->first << " " << it->second << std::endl ;
	stream.close() ;
}

void display_graph(const Graph & g,
	const std::string & name,
	std::set<my_edge_t> special_edges = std::set<my_edge_t>(),
	std::set<my_vertex_t> special_vertices = std::set<my_vertex_t>())  
{
	std::ofstream  stream(name+".dot");	
	auto b  = stream.fail();
	draw_graph(g,stream,name,special_edges,special_vertices);
	stream.close();
	std::system( ("dot -Tsvg "+name+ ".dot > " + name+".svg").c_str());
}

void display_subgraph(Graph &g,Graph &subgraph,std::string name)
{
	auto v = std::set<my_vertex_t>();
	std::transform(boost::vertices(subgraph).first,boost::vertices(subgraph).second,std::inserter(v,v.begin()),[&subgraph](Graph::vertex_descriptor a){ return subgraph[a] ;});
	auto e = std::set<my_edge_t>();
	std::transform(boost::edges(subgraph).first,boost::edges(subgraph).second, std::inserter(e,e.begin()),[&subgraph](Graph::edge_descriptor a){ return subgraph[a] ;});
	display_graph(g,name,e,v) ;
}
